package ferp.android.views.table.element.tutorial;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import ferp.android.GUI;
import ferp.android.views.EnhancedTextView;
import ferp.android.views.table.TableView;
import ferp.android.views.table.element.Element;

/**
 * User: olegs
 * Date: 28/01/2015 22:48
 */
public class TutorialNote extends Element<ScrollableTextView>
{
  public TutorialNote(Context context, TableView parent, View.OnClickListener ocl)
  {
    super(context, parent);

    view.setOnClickListener(ocl);
    // make view visible to avoid problems with animation
    view.setVisibility(View.VISIBLE);

    //view.setTextColor(0xFFFFFF00);
  }

  @Override
  public void layout()
  {
    if (area == null)
      view.layout(0, 0, 0, 0);
    else
    {
      view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

      int mw = view.getMeasuredWidth(), mh = view.getMeasuredHeight();
      int l = area.left, r = area.right, t = area.top, b = area.bottom;
      int msw = View.MeasureSpec.UNSPECIFIED, msh = View.MeasureSpec.UNSPECIFIED;

      if (mw < area.width())
      {
        l = GUI.center(mw, area.left, area.right);
        r = l + mw;
      }
      else
      {
        msw = View.MeasureSpec.makeMeasureSpec(area.width(), View.MeasureSpec.EXACTLY);

        // remeasure the view to get proper measured height as it might changed since
        // view was 'cut' to area's width
        view.measure(msw, msh);
        mh = view.getMeasuredHeight();
      }

      if (mh < area.height())
      {
        t = GUI.center(mh, area.top, area.bottom);
        b = t + mh;
      }
      else
        msh = View.MeasureSpec.makeMeasureSpec(area.height(), View.MeasureSpec.EXACTLY);

      view.measure(msw, msh);
      view.layout(l, t, r, b);
    }
  }

  @Override
  public void hide()
  {
    area = null;
    layout();
  }

  public void show(Rect area, String text)
  {
    EnhancedTextView etv = (EnhancedTextView)view.getChildAt(0);

    // assign new area and text
    this.area = area;

    view.scrollTo(view.getScrollX(), 0);
    // determine text height to properly size the images
    etv.setText(text);

    layout();
    fadeIn();
  }

  @Override
  protected ScrollableTextView create(Context context, TableView parent) {return new ScrollableTextView(context);}

  private void fadeIn()
  {
    Animation a = new AlphaAnimation(0, 1);

    a.setInterpolator(di);
    a.setDuration(ANIMATION_DURATION);
    a.setFillAfter(true);

    a.setAnimationListener(new Animation.AnimationListener()
    {
      @Override
      public void onAnimationStart(Animation animation) {view.bringToFront();}

      @Override
      public void onAnimationEnd(Animation animation)
      {
        animation.cancel();
        animation.setAnimationListener(null);
        view.clearAnimation();
      }

      @Override
      public void onAnimationRepeat(Animation animation) {}
    });

    view.startAnimation(a);
  }

  private static final long ANIMATION_DURATION = 250;

  private final DecelerateInterpolator di = new DecelerateInterpolator();
}
